package com.luzhikun.exer.util;

/**
 * ClassName: JDBCUtil
 * Description:
 * date: 2021/4/28 21:08
 *
 * @author luzhikun
 * @since JDK 1.8
 */

import java.sql.*;

/**
 * jdbc实现类
 */
public class JDBCUtil {

    //1.加载驱动
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /*
     * 连接方法
     *
     * */
//    private static Connection connection;
    public  Connection getConnection() throws SQLException {
        String url = "jdbc:mysql:///springdb";
        String root = "root";
        String password = "140489lu";
        Connection connection = DriverManager.getConnection(url, root, password);
        //返回获取到的连接
        return connection;
    }


    /*
     *
     * 关闭操作
     * */
    public static void closeAll(Connection connection, PreparedStatement pst, ResultSet rs) throws SQLException {
        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
        if (pst != null && !pst.isClosed()) {
            pst.isClosed();
        }
        if (rs != null && rs.isClosed()) {
            rs.close();
        }

    }

}
